package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Carousel;
import com.example.demo.entity.Food;
import com.example.demo.mapper.CarouselMapper;
import com.example.demo.service.CarouselService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* @author Atanycosts
* @description 针对表【carousel】的数据库操作Service实现
* @createDate 2023-04-19 17:02:44
*/
@Service
@Scope("prototype")
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel>
    implements CarouselService {

    @Autowired
    @Lazy
    private CarouselService carouselService;


    /**
     * 获取轮播图列表
     * @return 轮播图列表
     */
    @Override
    public Page<Carousel> getCarouselList(int page, int pageSize, String title) {

        //构造分页构造器
        Page<Carousel> pageInfo = new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(title), Carousel::getTitle,title);
        //添加排序条件
        queryWrapper.orderByDesc(Carousel::getUpdateTime);

        //执行查询
        carouselService.page(pageInfo,queryWrapper);
        return pageInfo;

    }

    /**
     * 根据轮播图id获取轮播图信息
     * @param id 轮播图id
     * @return 轮播图信息
     */
    @Override
    public Carousel getCarouselById(Long id) {
        Carousel byId = this.getById(id);

        LambdaQueryWrapper<Carousel> carouselLambdaQueryWrapper = new LambdaQueryWrapper();
        carouselLambdaQueryWrapper.eq(Carousel::getId, byId.getId());
        Carousel one = carouselService.getOne(carouselLambdaQueryWrapper);
        return one;
    }

    /**
     * 新增轮播图
     * @param carousel 轮播图信息
     * @return 是否成功新增轮播图
     */
    @Override
    public void addCarousel(Carousel carousel) {

        this.save(carousel);

    }

    /**
     * 删除轮播图
     * @param id 轮播图id
     * @return 是否成功删除轮播图
     */
    @Override
    public boolean deleteCarousel(Long id) {
        return false;
    }

    /**
     * 修改轮播图
     * @param carousel 轮播图信息
     * @return 是否成功修改轮播图
     */
    @Override
    public void updateCarousel(Carousel carousel) {

        this.updateById(carousel);
    }
}




